package array;

import java.util.Arrays;
import java.util.HashSet;
import java.util.TreeSet;

public class LongestConsecutive {
    public static void main(String[] args) {
        LongestConsecutive longestConsecutive = new LongestConsecutive();
        int solution = longestConsecutive.solution(new int[]{100,4,200,1,3,2});
        System.out.println(solution);
    }
    public int solution(int[] nums) {
        if(nums == null || nums.length == 0) return 0;
        HashSet<Integer> treeSet = new HashSet<>();
        for (int num : nums) {
            treeSet.add(num);
        }
        Integer[] array = treeSet.toArray(new Integer[treeSet.size()]);
        Arrays.sort(array);
        int max = 0;
        int flag = 0;
        for (int l = 1; l < array.length; l++) {
            if(array[l] - array[l - 1] != 1) {
                flag = l;
            }
            if(flag == 0) {
                flag = l;
            }
            max = Math.max(max, l - flag);
        }
        return max + 1;
    }
}
